package Server.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.omg.CORBA.ORB;

import Server.DAO.VWorldDao;
import VWorld.AdminPOA;
import VWorld.Avatar;
import VWorld.Room;

public class AdminImpl extends AdminPOA { 
	private ORB orb; 
	public void setORB(ORB orb_val) { orb = orb_val; }
	public String sayHello() { return "\nHello world !!\n"; } 
	public void shutdown() { orb.shutdown(false); }
	
	@Override
	public boolean isAdmin(Avatar admin) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public Avatar[] getAllAvatars() {
		Avatar tab[] = new Avatar[0];		
		
		try {
			VWorldDao.connection();
			Statement stm = VWorldDao.con.createStatement();
			String requete;
			requete = "SELECT COUNT(*) FROM Avatar WHERE isAdmin=0";			
			stm.execute(requete);
			ResultSet rs = stm.getResultSet();
			
			rs.next();

			int nbUser = rs.getInt(1);
			if(nbUser > 0){
				tab = new Avatar[nbUser];
				
				requete = "SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId, estConnecte FROM Avatar WHERE isAdmin=0";
				
				stm.execute (requete); 
				rs = stm.getResultSet();		
				
				int cpt = 0;
				while(rs.next()){
					boolean isAdmin;
					boolean isConnected;
					
					if(rs.getInt("isAdmin")==0){
						isAdmin= false;
					}else{
						isAdmin= true;
					}
					
					if(rs.getInt("estConnecte")==0){
						isConnected= false;
					}else{
						isConnected= true;
					}
				
					tab[cpt] = new Avatar(rs.getInt("avatar_id"),
							VWorld.Height.from_int(rs.getInt("height")), 
							VWorld.Humor.from_int(rs.getInt("humor")), 
							VWorld.Sex.from_int(rs.getInt("sex")), 
							rs.getString("name"), 
							rs.getString("password"), 
							rs.getString("email"), 
							isAdmin, 
							rs.getInt("currentRoomId"),
							isConnected
						);
					
					cpt++;
				}
			}			
		} catch (SQLException e) {
			System.out.println("Erreur de deconnection...");
			e.printStackTrace();
		} 
		
		return tab;
	}
	
	@Override
	public Avatar[] getAvatarConnected() {
		Avatar tab[] = new Avatar[0];		
		
		try {
			VWorldDao.connection();
			Statement stm = VWorldDao.con.createStatement();
			String requete;
			requete = "SELECT COUNT(*) FROM Avatar WHERE isAdmin=0 AND estConnecte=1";
			stm.execute(requete);
			ResultSet rs = stm.getResultSet();
			
			rs.next();

			int nbUser = rs.getInt(1);
			if(nbUser > 0){
				tab = new Avatar[nbUser];
				
				requete = "SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId, estConnecte FROM Avatar WHERE isAdmin=0 AND estConnecte=1";
				
				stm.execute (requete); 
				rs = stm.getResultSet();		
				
				int cpt = 0;
				while(rs.next()){
					boolean isAdmin;
					boolean isConnected;
					
					if(rs.getInt("isAdmin")==0){
						isAdmin= false;
					}else{
						isAdmin= true;
					}
					
					if(rs.getInt("estConnecte")==0){
						isConnected= false;
					}else{
						isConnected= true;
					}
				
					tab[cpt] = new Avatar(rs.getInt("avatar_id"),
							VWorld.Height.from_int(rs.getInt("height")), 
							VWorld.Humor.from_int(rs.getInt("humor")), 
							VWorld.Sex.from_int(rs.getInt("sex")), 
							rs.getString("name"), 
							rs.getString("password"), 
							rs.getString("email"), 
							isAdmin, 
							rs.getInt("currentRoomId"),
							isConnected
						);
					
					cpt++;
				}
			}			
		} catch (SQLException e) {
			System.out.println("Erreur de deconnection...");
			e.printStackTrace();
		} 
		
		return tab;
	}
	@Override
	public Avatar[] getAvatarInRoom(Room room) {
		Avatar tab[] = new Avatar[0];		
		
		try {
			VWorldDao.connection();
			Statement stm = VWorldDao.con.createStatement();
			String requete;
			requete = "SELECT COUNT(*) FROM Avatar WHERE isAdmin=0 AND currentRoomId=" + room.room_id;			
			stm.execute(requete);
			ResultSet rs = stm.getResultSet();
			
			rs.next();

			int nbUser = rs.getInt(1);
			if(nbUser > 0){
				tab = new Avatar[nbUser];
				
				requete = "SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId, estConnecte FROM Avatar WHERE isAdmin=0 AND currentRoomId=" + room.room_id;
				
				stm.execute (requete); 
				rs = stm.getResultSet();		
				
				int cpt = 0;
				while(rs.next()){
					boolean isAdmin;
					boolean isConnected;
					
					if(rs.getInt("isAdmin")==0){
						isAdmin= false;
					}else{
						isAdmin= true;
					}
					
					if(rs.getInt("estConnecte")==0){
						isConnected= false;
					}else{
						isConnected= true;
					}
				
					tab[cpt] = new Avatar(rs.getInt("avatar_id"),
							VWorld.Height.from_int(rs.getInt("height")), 
							VWorld.Humor.from_int(rs.getInt("humor")), 
							VWorld.Sex.from_int(rs.getInt("sex")), 
							rs.getString("name"), 
							rs.getString("password"), 
							rs.getString("email"), 
							isAdmin, 
							rs.getInt("currentRoomId"),
							isConnected
						);
					
					cpt++;
				}
			}			
		} catch (SQLException e) {
			System.out.println("Erreur de deconnection...");
			e.printStackTrace();
		} 
		
		return tab;
	}
	

}
